Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

I want to copy datagridview data to dataset without using a database. I want to write a class.

Please help me to solve this problem .
:confused::confused::confused::confused::rose:

I want to copy data only...

.
.
.
Please help me...
Posted
Updated 6-Mar-10 23:01pm
v2

1 solution

Couldn't guess out why you need a class to do the same! Still, here it is...

You can get like this:

C#
//If DataSource of GridView1 is a DataTable
DataTable dt = (DataTable)GridView1.DataSource;
//Now add this table to DataSet


If you want to make some changes while porting the data, you have to loop through. You have to manually create columns first, then loop through all the rows.

C#
//This is just a Psuedocode
DataTable dt = new DataTable();
//Add columns here
dt.Columns.Add(); //or something similar
//go on till all the columns are added
//loop and second loop
foreach(GridViewRow gvr in GridView1.Rows)
{
   DataRow dr = dt.NewRow();
   
   //second loop - looping all columns
   for(int i = 0; i < gvr.Rows.Count; ++i)
   {
      dr[i] = gvr.Cells[i].Text;
   }
}
//Add datatable to Dataset as you want here


Now if you want, you can put this piece of code in a class and expose it. Call it when you need to.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900